<?php
namespace app\commission\model;

use app\BaseModel;
use think\facade\Db;

class Cash extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_commission_cash';


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 09:35
     * @功能说明:创建
     */
    public function cashAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-22 09:40
     * @功能说明:佣金详情
     */
    public function cashInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }

    /**
     * @author chenniang
     * @DataTime: 2020-04-22 09:58
     * @功能说明:初始化一下
     */
    public function initData($dis){

        $info = $this->cashInfo($dis);

        if(empty($info)){

            $this->cashAdd($dis);
        }

        return true;
    }


    /**
     * @param $dis
     * @param $price
     * @功能说明:增减佣金
     * @author chenniang
     * @DataTime: 2020-04-22 09:39
     */
    public function incOrDecCash($dis,$price,$type=1){

        $info = $this->cashInfo($dis);

        if(empty($info)){

            $this->cashAdd($dis);
        }
        if($type==1){
            //增加佣金
            $res = $this->where($dis)->update(['total_cash'=>Db::raw("total_cash+$price")]);
        }else{
            //减佣金
            $res = $this->where($dis)->update(['total_cash'=>Db::raw("total_cash-$price")]);
        }
        return $res;
    }








}